package com.deerlive.testplayer;

import android.content.Context;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.deerlive.wbs.EncodeType;
import com.deerlive.wbs.WbsParams;
import com.deerlive.wbs.WbsPlayer;
import com.deerlive.wbs.WbsVideoStateChangeListener;

public class MainActivity extends AppCompatActivity {

    SurfaceView sfv ;
    WbsPlayer player;
    EditText tv;
    WbsParams p;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sfv = (SurfaceView) findViewById(R.id.surface);
        tv = (EditText)findViewById(R.id.sample_text);



    }

    @Override
    protected void onResume() {
        super.onResume();

        p = new WbsParams();
        p.setCodecId(EncodeType.VIDEO_ID_MPEG1VIDEO);
        p.setUrl(tv.getText().toString());
        p.setScaleMode(WbsParams.FITXY);
        p.setRotate(WbsParams.ROTATE_90);
        player = WbsPlayer.getInstance();
        player.setParams(p);
        player.setRender(sfv);
        player.setWbsVideoStateChangeListener(new WbsVideoStateChangeListener() {
            @Override
            public void onStateChange(int stateCode) {
                Log.i("+++++++++++++++++",stateCode+"");
            }
        });
        player.init();
    }

    public void play(View v){
        if(player == null){
            Toast.makeText(this,"please invoke init() first",Toast.LENGTH_LONG).show();
            return;
        }
        p.setUrl(tv.getText().toString());
        player.play();
    }

    public void pause(View v){

        if(player != null){
            player.destroy();
        }
        player = null;
    }

    public void switchs(View v){
        player.destroy();


        new Handler().postDelayed(new Runnable(){
            public void run() {
                player = null;
                player = WbsPlayer.getInstance();
                player.setParams(p);
                player.setRender(sfv);
                player.setWbsVideoStateChangeListener(new WbsVideoStateChangeListener() {
                    @Override
                    public void onStateChange(int stateCode) {
                        Log.i("+++++++++++++++++",stateCode+"");
                    }
                });
                player.init();
                p.setUrl("ws://pili-live-rtmp.whensup.cn:1250/whensup/60001.wsts");
                player.play();
            }
        }, 1000);

    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {

        return super.onCreateView(name, context, attrs);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(player != null){
            player.destroy();
        }
    }
}
